package com.sbl.bajw.pc.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.common.web.WebContext;
import com.sbl.bajw.mapper.OperationLogModelDao;
import com.sbl.bajw.mapper.collectLogModelDao;
import com.sbl.bajw.pc.model.OperationLogModel;
import com.sbl.bajw.pc.model.collectLogModel;
import com.sbl.bajw.util.FileUpload;
import com.sbl.bajw.util.LogUtil;

@Service
public class collectLogModelService {

	@Resource
	private collectLogModelDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	@Resource
	private OperationLogModelDao op_dao;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("collect");
	}
	
	@Transactional(readOnly = false)
	public void insert(collectLogModel model) throws MessageException{
		try {
			log.info(model.getCreateBy() + "图片入库");
			uploadFile(model);
			log.info(model.getCreateBy() + "图片入库成功，属性入库");
			dao.insert(model);
			log.info(model.getCreateBy() + "属性入库成功");
		} catch (Exception e) {
			log.info(model.getCreateBy() + "文件上传异常：" + e.getMessage());
			e.printStackTrace();
			throw new MessageException("文件上传失败", "3");
		}
		
		//日常采集日志入库操作日志
		OperationLogModel entity = new OperationLogModel();
		entity.setOperationuserid(model.getCreateBy());
		entity.setOperationcontent("日常采集日志入库");
		entity.setOperationname("日常采集日志插入");
		entity.setActionid("1");
		op_dao.insert(entity);
		
	}
	
	public HashMap<String, Object> getModel(String id){
		HashMap<String, Object> model = dao.getModel(id);
		return model;
	}
	
	public List<HashMap<String, Object>> getAttfileByCollectLogId(String id){
		List<HashMap<String, Object>> list = dao.getAttfileByCollectLogId(id);
		return list;
	}
	
	public String getCollectLogList(HashMap<String, Object> parm){
		String json = gridDao.selectPage(collectLogModelDao.class, parm, "getCollectLogList");
		
		//日常采集日志列表查询操作日志
		OperationLogModel entity = new OperationLogModel();
		entity.setOperationuserid(SessionUnit.getUserId());
		entity.setOperationcontent("日常采集日志列表采集");
		entity.setOperationname("日常采集日志列表查询");
		entity.setActionid("4");
		op_dao.insert(entity);
		
		return json;
	}
	
	public List<HashMap<String, Object>> downloadCollectLogList(HashMap<String, Object> parm){
		List<HashMap<String, Object>> list = dao.getCollectLogList(parm);
		
		//日常采集日志列表导出操作日志
		OperationLogModel entity = new OperationLogModel();
		entity.setOperationuserid(SessionUnit.getUserId());
		entity.setOperationcontent("日常采集日志列表导出");
		entity.setOperationname("日常采集日志列表导出");
		entity.setActionid("5");
		op_dao.insert(entity);
				
		return list;
	}
	
	public String uploadFile(collectLogModel model) throws Exception {
		String id = dao.getCollectLogId();

		if (id == null || id.equals("")) {
			throw new MessageException("ID不能为空", "2");
		}
		
		String resultPath = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		for(int i = 1; i < 10; i++){
			FileItem item = RequestUnit.getFileItem("img" + i);
			if (item != null) {
				String path = WebContext.getRequest().getSession()
						.getServletContext().getInitParameter("UploadPath");
				String dataStr = sf.format(Calendar.getInstance().getTime());
				resultPath = "images/upload/collect/" + FileUpload.saveFile(item, path + "/images/upload/collect/", dataStr + "_img_" + id);
				log.info(SessionUnit.getSessionId() + "图片上传成功:" + resultPath);
				HashMap<String, String> parm = new HashMap<String, String>();
				parm.put("path", resultPath);
				parm.put("groupid", id);
				parm.put("businessType", "3");
				parm.put("filetype", "img");
				dao.insertAttfile(parm);
				model.setPicfileids(id);
			} else {
				log.info(SessionUnit.getSessionId() + "没有图片数据");
			}
		}

		FileItem record = RequestUnit.getFileItem("voice");
		if (record != null) {
			String path = WebContext.getRequest().getSession().getServletContext().getInitParameter("UploadPath");
			String dataStr = sf.format(Calendar.getInstance().getTime());
			resultPath = "voice/upload/collect/" + FileUpload.saveFile(record, path + "/voice/upload/collect/", dataStr + "_record_" + id);
			log.info(SessionUnit.getSessionId() + "录音上传成功:" + resultPath);
			HashMap<String, String> parm = new HashMap<String, String>();
			parm.put("path", resultPath);
			parm.put("groupid", id);
			parm.put("businessType", "3");
			parm.put("filetype", "record");
			dao.insertAttfile(parm);
			model.setRecordid(id);
		} else {
			log.info(SessionUnit.getSessionId() + "没有录音数据");
		}
		return resultPath;
	}
}
